import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:ecy_app_flutter/ecycommon/EcyCommonWidgets.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../../constant/color_constant.dart';
import '../../EcyRouter.dart';
import 'package:ecy_app_flutter/ecycommon/EcyRequestBaseManager.dart';
import 'package:flutter_styled_toast/flutter_styled_toast.dart';

class EcyOtherMobileLoginPage extends EcyCommonBaseWidget {
  EcyOtherMobileLoginPage({required super.key});

  @override
  _EcyOtherMobileLoginPage createState() {
    return _EcyOtherMobileLoginPage(this);
  }
}

class _EcyOtherMobileLoginPage extends EcyCommonBaseWidgetState {
  String mobile = "18251846048";
  bool checked = true;
  String yysName = "中国电信提供认证服务";
  bool canNext = false;
  late EcyCommonBaseWidget controller;
  _EcyOtherMobileLoginPage(EcyCommonBaseWidget vc) {
    controller = vc;
  }
  @override
  void initState() {
    super.initState();
    // requestData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: ColorConstant.White,
        appBar: AppBar(
          backgroundColor: ColorConstant.White,
          title: Text(this.title),
        ),
        body: Column(crossAxisAlignment: CrossAxisAlignment.center, children: [
          Container(
            height: 140,
          ),
          const Image(
              image: AssetImage('assets/images/login_app_icon.png'),
              width: 80,
              height: 80),
          SizedBox(width: MediaQuery.of(context).size.width, height: 48),
          Container(
              margin: const EdgeInsets.only(
                  left: 26, right: 26, top: 0, bottom: 0.0),
              child: MyTextFieldWidget(
                  mobile2: mobile,
                  updateMessageCallback2: this.onUpdateMobile)),
          SizedBox(width: MediaQuery.of(context).size.width, height: 16),
          Wrap(
            direction: Axis.horizontal,
            spacing: 10,
            children: [
              GestureDetector(
                onTap: () => {
                  setState(() {
                    checked = !checked;
                  }),
                  print('checkedt' + checked.toString()),
                },
                child: Image(
                    image: AssetImage(checked
                        ? 'assets/images/login_checked.png'
                        : 'assets/images/login_unchecked.png'),
                    width: 34,
                    height: 34),
              ),
              Container(
                width: 350,
                child: GestureDetector(
                    onTap: () => print('Tapped outside highlighted text'),
                    child: RichText(
                      text: TextSpan(
                        children: [
                          TextSpan(
                            text: '新手机号将自动注册，已详细阅读同意',
                            style: TextStyle(
                                color: Color.fromARGB(255, 140, 140, 140),
                                fontSize: 16),
                          ),
                          TextSpan(
                              recognizer: TapGestureRecognizer()
                                ..onTap = () {
                                  print('Tapped outside highlighted text22');
                                },
                              text: '《使用条款和个人信息保护政策》',
                              style: TextStyle(
                                  color: ColorConstant.CommonColor,
                                  fontSize: 16)),
                        ],
                      ),
                    )),
              ),
            ],
          ),
          GestureDetector(
              onTap: () {
                if (canNext) {
                  EcyRouter.push(context, EcyRouter.inputCodeLoginPage,
                      {"mobile": mobile});
                }
              },
              child: Container(
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(26),
                  color: canNext
                      ? ColorConstant.CommonColor
                      : ColorConstant.CommonColorLight,
                ),
                margin: const EdgeInsets.only(
                    left: 22, right: 22, top: 24, bottom: 10.0),
                width: MediaQuery.of(context).size.width - 44,
                height: 52,
                alignment: Alignment.center,
                child: const Text('下一步',
                    textAlign: TextAlign.center,
                    style: TextStyle(
                        fontFamily: "PingFang SC",
                        fontWeight: FontWeight.w700,
                        fontSize: 18,
                        color: ColorConstant.White)),
              ))
        ]));
  }

  //private
  void onUpdateMobile(String newValue) {
    setState(() {
      mobile = newValue;
      if (mobile.length == 11) {
        canNext = true;
      } else {
        canNext = false;
      }
    });
  }

  void requestData() async {}
}

class MyTextFieldWidget extends StatefulWidget {
  final String mobile2;
  final ValueChanged<String> updateMessageCallback2;
  MyTextFieldWidget(
      {required this.mobile2, required this.updateMessageCallback2});

  @override
  _MyTextFieldWidgetState createState() => _MyTextFieldWidgetState(
      mobile: mobile2, updateMessageCallback: updateMessageCallback2);
}

class _MyTextFieldWidgetState extends State<MyTextFieldWidget> {
  final TextEditingController _controller = TextEditingController();
  final String mobile;
  ValueChanged<String> updateMessageCallback;
  _MyTextFieldWidgetState(
      {required this.mobile, required this.updateMessageCallback});
  @override
  Widget build(BuildContext context) {
    return TextField(
        inputFormatters: [
          FilteringTextInputFormatter.digitsOnly,
          LengthLimitingTextInputFormatter(11)
        ],
        decoration: InputDecoration(
            border: InputBorder.none, // 移除默认边框
            focusedBorder: UnderlineInputBorder(
              // 获取焦点时的下划线
              borderSide: BorderSide(
                color: ColorConstant.CommonColorGray, // 修改下划线颜色
                width: 1.0, // 修改下划线宽度
              ),
            ),
            enabledBorder: UnderlineInputBorder(
              // 默认状态下的下划线
              borderSide: BorderSide(
                color: ColorConstant.CommonColorGray, // 修改下划线颜色
                width: 1.0, // 修改下划线宽度
              ),
            ),
            hintText: '请输入您的手机号',
            hintStyle: TextStyle(
                fontFamily: "PingFag SC",
                fontWeight: FontWeight.w700,
                fontSize: 18,
                color: ColorConstant.CommonColorGray)),
        textAlign: TextAlign.start,
        style: TextStyle(
            fontFamily: "PingFag SC",
            fontWeight: FontWeight.w700,
            fontSize: 18,
            color: ColorConstant.CommonColorBlack),
        onEditingComplete: () {
          // 当用户点击完成按钮时，这个函数会被调用;
          // 你可以在这里添加你的逻辑，比如验证输入或提交数据
        },
        onChanged: (value) {
          // 这个回调函数会在文本变化时不断被调用
          print('文本变化: $value');
          updateMessageCallback(value);
        });
  }

  @override
  void dispose() {
    // 清理控制器，避免内存泄漏
    _controller.dispose();
    super.dispose();
  }
}
